<template>
  <div class="update-gender-container">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '../../api/profile'

export default {
  name: 'updateGender',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },
  methods: {
    async onConfirm (value, index) {
      this.$toast.loading({
        message: '更新中……',
        forbidClick: true
      })
      await updateUserProfile({
        gender: this.defaultIndex
      })
      this.$emit('input', this.defaultIndex)
      this.$emit('close')
      this.$toast.success('更新成功！')
    },
    onChange (picker, value, index) {
      this.defaultIndex = index
    }
  }
}
</script>

<style scoped>

</style>
